//
//  CSYMyMicroBlogModel.swift
//  CSYMicroBlockSina
//
//  Created by 姚彦兆 on 15/11/14.
//  Copyright © 2015年 姚彦兆. All rights reserved.
//

import UIKit

class CSYMyMicroBlogModel: NSObject {

    var created_at : String?
    
    var id : Int64 = 0
    
    var text : String?
    
    var source : String?
    
    var userMessageModel : CSYUserMessageModel?
    
    var retweetedMessageModel : CSYMyMicroBlogModel?
    
    var getSource : String?{
    
        if retweetedMessageModel != nil{
        
            return retweetedMessageModel?.source
        
        }
        
        return source
    
    }
    
    //微博图片的URL
    var pic_urls : [[String : String]]?
    
    var picURLArray : [NSURL]? {
    
        guard let urls = pic_urls else{
        
            return nil
        
        }
        
        var urlArray : [NSURL] = [NSURL]()
        
        for urlDict in urls{
            
            if let url : NSURL? = NSURL(string: urlDict["thumbnail_pic"]!){
                
                urlArray.append(url!)
            
            }
            
        }
        
        return urlArray
    
    }
    
    //确定是转发还是原创的图片
    var blogPictures : [NSURL]? {
    
        if retweetedMessageModel != nil {

            
            return retweetedMessageModel?.picURLArray
            
//            if picURLArray?.count == 0 {
//            
//                return (retweetedMessageModel?.picURLArray)!
//            
//            }else{
//            
//                return picURLArray! + (retweetedMessageModel?.picURLArray)!
//            
//            }
        
        }
        
        return picURLArray!
    
    }
    
    init(dict: [String : AnyObject]){
    
        super.init()
        
        self.setValuesForKeysWithDictionary(dict)
    
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        
        if key == "user" {
        
            if let dict = value as? [String : AnyObject]{
            
                userMessageModel = CSYUserMessageModel(dict : dict)
            
            }
            
            return
        
        }
        
        if key == "retweeted_status" {
        
            if let dict = value as? [String : AnyObject]{
            
                retweetedMessageModel = CSYMyMicroBlogModel(dict: dict)
            
            }
            
            return
        
        }
        
        super.setValue(value, forKey: key)
        
    }
    
    override var description : String {
    
        let keys = ["created_at" , "id" , "text" , "source"]
        
        return dictionaryWithValuesForKeys(keys).description
    
    }
    
    
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
}
